﻿using Google.Protobuf;
using MP_Game.Protos.V1;

namespace MP_VerifyServer
{
    public static class LoginEndpoint
    {
        public static void Map(WebApplication app)
        {
            app.MapPost("/login", Handle);
        }

        private static async Task Handle(HttpContext ctx, IVerifyService svc)
        {
            try
            {
                LogMsg.Warn("login接口被调用！");
                LoginReq req;
                using (var ms = new MemoryStream())
                {
                    await ctx.Request.Body.CopyToAsync(ms);
                    ms.Position = 0;
                    req = LoginReq.Parser.ParseFrom(ms);
                }
                var rsp = svc.Login(req);
                ctx.Response.ContentType = "application/x-protobuf";
                await ctx.Response.Body.WriteAsync(rsp.ToByteArray());
            }
            catch (Exception ex)
            {
                LogMsg.Warn($"Login接口崩溃：{ex.Message}");
            }
        }
    }
}
